<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片</title>
    <link rel="stylesheet" th:href="@{/css/index.css}">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}"/>
    <script th:src="@{/layui/layui.js}"></script>

</head>
<body>

<div class="cards" th:each="img : ${imgList}">
    <div class="col-md-4 col-sm-6 col-lg-3">
        <div class="card"><img th:src="${img.getImgUrl()}" width="400px" height="200px" class="img-rounded" alt="暂无图片"></div>
        <a th:href="@{/album/deleteImg(id=${img.getId()},albumId=${albumId})}">⛔删除此图片</a>
        <hr/>
    </div>

</div>
<h2 align="center">
    <font style="color: hotpink">not have any more~</font></h2>

<form th:if="${now_user.getId()}==${albumUserId}" class="layui-form" action="">
    <!-- 这里设置一个隐藏域，用来存放我们的图片的地址 -->
    <input type="hidden" id="imagePath" name="imagePath" value="">
    <input type="hidden" id="albumId" name="albumId" th:value="${albumId}">
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">上传按钮</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="test1">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">图片预览</label>
        <div class="layui-input-block">
            <img id="image">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="upload">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>

</form>
<font th:if="${now_user.getId()}!=${albumUserId}" >nonono</font>



<script type="text/javascript" src="../js/index.js"></script>

<!--<footer class="foot">-->
<!--    <p>@2021-2022 | BY:Roydon</p>-->
<!--    <p>网页版权归Roydon所有</p>-->
<!--    <p>Man who has a settled purpose will surely succeed</p>-->
<!--</footer>-->
</body>

<script>
    layui.use('upload', function () {
        let upload = layui.upload;
        let $ = layui.$;
        //执行实例
        let uploadInst = upload.render({
            elem: '#test1' //绑定元素
            , url: '/upload/image' //上传接口
            , done: function (res) {
                if (res.code == 0) {
                    layer.msg("上传成功");
                    //获得图片的路径
                    let src = res.data.src;
                    //将图片进行回显
                    $("#image").attr("src", src);
                    //将图片的地址赋值给 form的隐藏域
                    $("#imagePath").attr("value", src);
                }
            }
            , error: function () {
                //请求异常回调
            }
        });
    });


    layui.use('form', function () {
        let form = layui.form;
        let $ = layui.$;
        //监听表单的提交
        form.on('submit(upload)', function (data) {
            $.ajax({
                url: "/upload/addUserImage",
                method: "post",
                data: data.field,
                dataType: "JSON",
                success: function (res) {
                    if (res == 1) {
                        layer.msg("上传成功");
                        window.location.href = "/album/myAlbum";

                    }
                }
            })
            return false;
        });
    });


</script>

</html>
